<?php
if(!class_exists('fs_content')){
	class fs_handler{
		
		function shortcodeHandler($atts){
			extract(shortcode_atts(array( //extract atts to their variables.
				'set'=>false
			), $atts));

			if($set){ //need a set Id.
				return fs_handler::handleSet($set);
			}else{
				return "You must specify a set ID";
			}
		}
		
		function handleSet($setid){ //creates html for the set
			$time_start = microtime(true);//timing
			
			//load options into variables.
			extract(unserialize(get_option("fs_options")));

			//create api, load cache, and an object for the set
			$token = get_option('fs_token');
			$api = new Phlickr_Api(FS_API_KEY, FS_API_SEC, $token);
			if(!$api->isAuthValid()) return "flickrSet is not authorised.";

			//load cached requests
			$cachefile = dirname(__FILE__) . '/fs_cache';
			$api->setCache(Phlickr_Cache::createFrom($cachefile));

			//create the set object
			$set = new Phlickr_Photoset($api, $setid);
			
			$r = '<div class="flickrsetwrap">';
			
			if($primary){
				$pimg = new Phlickr_Photo($api, $set->getPrimaryId());
				$r .= '<div class="primary">'.fs_handler::image($pimg, $psize);
				$r .= $thickbox ? fs_handler::thickboxLink($pimg, $set) : '';
				$r .= '</div>';
			}
			
			$r .= '<div class="flickrset"><ul>';
			
			foreach($set->getPhotoList()->getPhotos() as $photo){
				if($primary && $photo->getId()==$set->getPrimaryId()) continue;
				$r .= "\n<li class=\"flickrsetitem\">";
				$r .= fs_handler::image($photo, $size);
				$r .= $thickbox ? fs_handler::thickboxLink($photo, $set) : '';
				$r .= '</li>';
			}
			
		 	// serialize and save the cache file
		 	$api->getCache()->saveAs($cachefile);
		
 	 	 	$diff = microtime(true) - $time_start;
 	 	 	return $r . '</ul></div><br style="clear:both"/><!--'.$diff.'s--></div>';
		}
		
		//$photo is a Phlickr_Photo, return Flickr-friendly image html
		function image($photo, $size){
			$terms = array(
				"%photourl%"=> $photo->buildUrl(),
			 	"%thumb%"	=> $photo->buildImgUrl($size),
				"%title%"  	=> $photo->getTitle(),
			);

			$str = '<a href="%photourl%" title="%title% on flickr">'.
					'<img src="%thumb%" alt="%title%"/></a>';
					
			return str_replace(array_keys($terms), array_values($terms), $str);
		}
		
		function thickboxLink($photo, $set){
			
			$terms = array(
				"img" =>$photo->buildImgUrl(Phlickr_Photo::SIZE_500PX),
				"set" =>fs_handler::slug($set),
				"info" =>fs_handler::getTBTitle($photo)
			);
			
			$str = '<a class="thickbox" href="img" rel="set" title="info" >';
			
			$r = str_replace(array_keys($terms), array_values($terms), $str);
			
			return $r.'<img src="'.FS_BASE_URL.'magnifier_zoom_in.png"/></a>';
		}
		
		function getTBTitle($photo){
			$exif = $photo->getExif();
			
			$terms = array(
				"%title%"=> $photo->getTitle(),
			 	"|" 	 => " &#124; ",
			 	"%exp%"	 => $exif->getExposure(),
				"%fno%"	 => $exif->getAperture(),
				"%mm%"	 => $exif->getFocalLength(),
				"%iso%"	 => $exif->getIsoSpeed(),
			);
			
			$str = '%title%&lt;br/&gt;%mm%mm|&fnof;%fno%|%exp%s|IS0%iso%';
			
			return str_replace(array_keys($terms), array_values($terms), $str);
		}
		
		//used with lightbox, removes spaces, to lowercase.
		function slug($string){
			return strtolower(str_replace(' ', '', $string->getTitle()));
		}


		function header(){ //inserts a link to the css into the wp head.
			$href = FS_BASE_URL . 'flickrset.css';
			echo "\t".'<link rel="stylesheet" type="text/css" href="'.$href.'" />';
			$href = get_bloginfo('wpurl'). "/wp-includes/js/thickbox/thickbox.css";
			echo "\n\t".'<link rel="stylesheet" type="text/css" href="'.$href.'" />';
		}

		function init(){
			if ( !function_exists('wp_enqueue_script') || is_admin() )
				return;
			wp_enqueue_script('thickbox');
		}
	}
}
?>